﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Linq.Dynamic;

/// <summary>
/// Summary description for InventoryBL
/// </summary>
public class InventoryBL
{
	public InventoryBL()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public List<DAO.InventoryViewInfo> GetInventoryView(string productcode, string orderby)
    {
        string code;
        if (null == productcode || string.Empty == productcode || "00" == productcode)
        {
            code = string.Empty;
        }
        else
        {
            code = productcode.Trim();
        }
         
        string orderexpress;
        if (null == orderby || string.Empty == orderby)
        {
            orderexpress = "ProductCode";
        }
        else
        {
            orderexpress = orderby.Trim();
        }

        using (DAO.InventoryDataDataContext db = new DAO.InventoryDataDataContext(PublicDefine.SQLConnectString))
        {
            List<DAO.InventoryViewInfo> inventoryview;

            if (null == code || string.Empty == code)
            {
                var query = db.InventoryViewInfos
                            .OrderBy(orderexpress);

                inventoryview = query.ToList<DAO.InventoryViewInfo>();
            }
            else
            {
                var result = db.InventoryViewInfos
                             .Where("ProductCode.StartsWith(@0)", code)
                             .OrderBy(orderexpress);
                inventoryview = result.ToList<DAO.InventoryViewInfo>();
            }
            
            return inventoryview;
        }
    }

    
}
